# 绘制各种图形
import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image

# 创建纯黑的背景图用来画图形
img_line = np.zeros((480, 640, 3), np.uint8)
img_rectangle = np.zeros((480, 640, 3), np.uint8)
img_circle = np.zeros((480, 640, 3), np.uint8)
img_ellipse = np.zeros((480, 640, 3), np.uint8)
img_polylines = np.zeros((480, 640, 3), np.uint8)
img_fillPolylines = np.zeros((480, 640, 3), np.uint8)
img_text = np.zeros((480, 640, 3), np.uint8)
# line(img,pt1,pt2,color,thickness,lineType,shift)
# img 图片对象
# pt1 起始点
# pt2 结束点
# color 颜色
# thickness 线宽
# lineType 线型：取值1,4,8,16，默认为8
# shift 坐标缩放比例

cv2.line(img_line, (10, 20), (300, 400), (0, 0, 255), 5, 4)
cv2.line(img_line, (10, 60), (300, 460), (0, 255, 255), 5, 16)

cv2.rectangle(img_rectangle, (10, 20), (30, 40), (255, 0, 0), 5, 4)
cv2.rectangle(img_rectangle, (30, 60), (330, 460), (0, 255, 255), 5, 16)
cv2.circle(img_circle, (320, 240), 50, (0, 0, 255), 5, 16)
cv2.ellipse(img_ellipse, (320, 240), (30, 30), 0, 0, 360, (0, 0, 255), 5, 16)
pts = np.array([(10, 20), (20, 30), (30, 40), (50, 100)], np.int32)
cv2.polylines(img_polylines, [pts], True, (0, 0, 255), 1, 16)
cv2.polylines(img_polylines, [pts], False, (0, 255, 255), 1, 16)
cv2.fillPoly(img_fillPolylines, [pts], (0, 0, 255), 16)
cv2.putText(img_text, 'Hello OpenCV', (50, 400), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
# cv2.imshow('line', img_line)
# cv2.imshow('rectangle', img_rectangle)
# cv2.imshow('circle', img_circle)
# cv2.imshow('ellipse', img_ellipse)

# cv2.imshow('polylines', img_polylines)
# cv2.imshow('fill_polylines', img_fillPolylines)
# cv2.imshow('text', img_text)
# 纯白色
img = np.full((500, 500, 3), fill_value=255, dtype=np.uint8)
# 导入字体文件
font = ImageFont.truetype('./fonts/simfang.ttf', 15)
# 创建一个pillow的图片
img_pillow = Image.fromarray(img)
draw = ImageDraw.Draw(img_pillow)
# 利用draw去绘制中文
draw.text((100, 250), '你好，我好，大家好', font=font, fill=(0, 0, 255, 100))
# 重新变回ndarray
img_text_new = np.array(img_pillow)
cv2.imshow('text', img_text_new)
cv2.waitKey(0)
cv2.destroyAllWindows()
